//
//  ICExtension.swift
//  ICFoundationFramework
//
//  Created by cuichengcheng on 2017/12/5.
//  Copyright © 2017年 cuichengcheng. All rights reserved.
//

import UIKit

class ICExtension: UIView {

    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */

}
private func roundbyunit(_ num: Double, _ unit: inout Double) -> Double {
    let remain = modf(num, &unit)
    if (remain > unit / 2.0) {
        return ceilbyunit(num, &unit)
    } else {
        return floorbyunit(num, &unit)
    }
}
private func ceilbyunit(_ num: Double, _ unit: inout Double) -> Double {
    return num - modf(num, &unit) + unit
}

private func floorbyunit(_ num: Double, _ unit: inout Double) -> Double {
    return num - modf(num, &unit)
}

private func pixel(_ num: Double) -> Double {
    var unit: Double
    switch Int(UIScreen.main.scale) {
    case 1: unit = 1.0 / 1.0
    case 2: unit = 1.0 / 2.0
    case 3: unit = 1.0 / 3.0
    default: unit = 0.0
    }
    return roundbyunit(num, &unit)
}
func createImg(color:UIColor, rect:CGRect) -> UIImage? {
    if rect.equalTo(.zero) {return nil}
    UIGraphicsBeginImageContextWithOptions(rect.size, false, UIScreen.main.scale)
    let ctx = UIGraphicsGetCurrentContext()
    color.set()
    ctx?.fill(rect)
    let img = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return img
}
func kt_drawRectWithRoundedCorner(radius: CGFloat,
                                          borderWidth: CGFloat,
                                          backgroundColor: UIColor,
                                          borderColor: UIColor?,
                                          size:CGSize) -> UIImage {
    let sizeToFit = CGSize(width: pixel(Double(size.width)), height: Double(size.height))
    let halfBorderWidth = CGFloat(borderWidth / 2.0)
    
    UIGraphicsBeginImageContextWithOptions(sizeToFit, false, UIScreen.main.scale)
    let ctx = UIGraphicsGetCurrentContext()
    borderWidth > 0.0 ? ctx?.setLineWidth(borderWidth) : nil
    borderColor != nil ? ctx?.setStrokeColor(borderColor!.cgColor) : nil
    ctx?.setFillColor(backgroundColor.cgColor)
    
    let width = sizeToFit.width, height = sizeToFit.height
    ctx?.move(to: CGPoint(x: width - halfBorderWidth, y: radius + halfBorderWidth))  // 开始坐标右边开始
    ctx?.addArc(tangent1End: CGPoint(x:width - halfBorderWidth, y:height - halfBorderWidth), tangent2End: CGPoint(x:width - radius - halfBorderWidth, y:height - halfBorderWidth), radius: radius)  // 右下角角度
    ctx?.addArc(tangent1End: CGPoint(x:halfBorderWidth, y:height - halfBorderWidth), tangent2End: CGPoint(x:halfBorderWidth, y:height - radius - halfBorderWidth), radius: radius)// 左下角角度
    ctx?.addArc(tangent1End: CGPoint(x:halfBorderWidth, y:halfBorderWidth), tangent2End: CGPoint(x:width - halfBorderWidth, y:halfBorderWidth), radius: radius) // 左上角
    ctx?.addArc(tangent1End: CGPoint(x:width - halfBorderWidth, y:halfBorderWidth), tangent2End: CGPoint(x:width - halfBorderWidth, y:radius + halfBorderWidth), radius: radius) // 右上角
    ctx?.drawPath(using: .fillStroke)
    let output = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return output!
}

extension UIView {
    @objc func kt_addCorner(radius: CGFloat) {
        self.kt_addCorner(radius: radius, borderWidth: 1, backgroundColor: UIColor.clear, borderColor: UIColor.black)
    }
    
    func kt_addCorner(radius: CGFloat,
                      borderWidth: CGFloat,
                      backgroundColor: UIColor,
                      borderColor: UIColor?) {
        let imageView = UIImageView(image: kt_drawRectWithRoundedCorner(radius: radius,
                                                                        borderWidth: borderWidth,
                                                                        backgroundColor: backgroundColor,
                                                                        borderColor: borderColor,
                                                                        size: self.bounds.size))
        self.insertSubview(imageView, at: 0)
    }
    
    
}

extension UIImageView {
    /**
     / !!!只有当 imageView 不为nil 时，调用此方法才有效果
     
     :param: radius 圆角半径
     */
    override func kt_addCorner(radius: CGFloat) {
        self.image = self.image?.kt_addCorner(radius: radius, self.bounds.size)
    }
}

extension UIImage {
    func kt_addCorner(radius: CGFloat, _ sizetoFit: CGSize) -> UIImage {
        let rect = CGRect(origin: CGPoint(x: 0, y: 0), size: sizetoFit)
        UIGraphicsBeginImageContextWithOptions(rect.size, false, UIScreen.main.scale)
        let ctx = UIGraphicsGetCurrentContext()
        ctx?.addPath(UIBezierPath(roundedRect: rect,
                                  byRoundingCorners: UIRectCorner.allCorners,
                                  cornerRadii: CGSize(width: radius, height: radius)).cgPath)
        ctx?.clip()
        self.draw(in: rect)
        ctx?.drawPath(using: .fillStroke)
        let output = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return output!
    }
    
    
    
    
}




